package test.my.com.myapplicationtest.BI_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import test.my.com.myapplicationtest.R;

/**
 * Author : cooper
 * Time :  2018/6/14 下午3:44
 * Description :  转换率
 */
public class BiZhlView extends View {

    private Context mContext;

    private int mWidth = 100;
    private int mHeight = 100;

    private float mWdithRate = 0.25f; // 竖线所在整个图的位置
    private int mVerticalSpace = 50;
    private float mItemHeight;
    private List<String> list = new ArrayList<>();

    private int mZhlTitleColor;
    private int mZhlValueColor;
    private int mZhlThemeColor;
    private int mPointRadius = 20; // 圆点半径
    private int mTitleSpace = 50;  // title间距
    private int mRectSpace = 30;   // 矩形左方间距
    private int mValueSpace = 20;  // 矩形右方数据间距
    private int mTitleSize = 50;  // 标题字体大小
    private int mValueSize = 50;  // 数据字体大小

    private Paint mPointPaint;
    private Paint mTitlePaint;
    private Paint mLinePaint;
    private Paint mRectPaint;
    private Paint mValuePaint;

    private Rect mTitleRect = new Rect();// 用于获取文字宽高
    private Rect mValueRect = new Rect();// 用于获取文字宽高
    private Rect mPercentRect = new Rect();// 用于获取文字宽高
    private float mStartLineY;//竖线的纵坐标
    private float mEndLineY;

    private List<ZhlEntity> mZhlEntityList = new ArrayList<>();

    public BiZhlView(Context context) {
        super(context);
    }

    public BiZhlView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BiZhlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.BiZhlView, defStyleAttr, 0);
        mZhlTitleColor = ta.getColor(R.styleable.BiZhlView_zhlTitleColor, Color.parseColor("#78849A"));
        mZhlValueColor = ta.getColor(R.styleable.BiZhlView_zhlValueColor, Color.parseColor("#657389"));
        mZhlThemeColor = ta.getColor(R.styleable.BiZhlView_zhlThemeColor, Color.parseColor("#3778F2"));
//        mAnimatorTime = ta.getInt(R.styleable.DynaYHstlView_animatorStlTime, 1000);
        ta.recycle();

        init(context);
    }

    private void init(Context context) {
        mPointPaint = new Paint();
        mPointPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPointPaint.setStyle(Paint.Style.FILL);
        mPointPaint.setColor(mZhlThemeColor);

        mTitlePaint = new Paint();
        mTitlePaint.setColor(mZhlTitleColor);
        mTitlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mTitlePaint.setTextSize(mTitleSize);

        mValuePaint = new Paint();
        mValuePaint.setColor(mZhlValueColor);
        mValuePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mValuePaint.setTextSize(mValueSize);
        mValuePaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "DINMittelschrift-Alternate.otf"));

        mLinePaint = new Paint();
        mLinePaint.setColor(Color.parseColor("#EBECF0"));
        mLinePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mLinePaint.setStrokeWidth(2);

        mRectPaint = new Paint();
        mRectPaint.setColor(mZhlThemeColor);
        mRectPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mRectPaint.setStyle(Paint.Style.FILL);

    }

    public void addData() {
        ZhlEntity zhlEntity = new ZhlEntity();
        zhlEntity.zhlPercent = 23.4f;
        zhlEntity.zhlTitle = "进价";
        zhlEntity.zhlValue = 300;

        ZhlEntity zhlEntity1 = new ZhlEntity();
        zhlEntity1.zhlPercent = 45.6f;
        zhlEntity1.zhlTitle = "放款";
        zhlEntity1.zhlValue = 1000;

        ZhlEntity zhlEntity2 = new ZhlEntity();
        zhlEntity2.zhlPercent = 33.4f;
        zhlEntity2.zhlTitle = "审核";
        zhlEntity2.zhlValue = 300;

        ZhlEntity zhlEntity3 = new ZhlEntity();
        zhlEntity3.zhlPercent = 63.4f;
        zhlEntity3.zhlTitle = "卖出";
        zhlEntity3.zhlValue = 100;

        mZhlEntityList.add(zhlEntity);
        mZhlEntityList.add(zhlEntity1);
        mZhlEntityList.add(zhlEntity2);
        mZhlEntityList.add(zhlEntity3);

        requestLayout();
        invalidate();
    }

    private void caculateSize(ZhlEntity zhlEntity) {
        mPointRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 200,
                getResources().getDisplayMetrics());
        mTitleSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 50,
                getResources().getDisplayMetrics());
        mRectSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 70,
                getResources().getDisplayMetrics());
        mValueSpace = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 160,
                getResources().getDisplayMetrics());
        mTitleSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 60,
                getResources().getDisplayMetrics());
        mValueSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 45,
                getResources().getDisplayMetrics());


        mTitlePaint.getTextBounds(zhlEntity.zhlTitle, 0, zhlEntity.zhlTitle.length(), mTitleRect);
        mValuePaint.getTextBounds(zhlEntity.zhlValue+"", 0, (zhlEntity.zhlValue+"").length(), mValueRect);
        mValuePaint.getTextBounds(zhlEntity.zhlPercent+"%", 0, (zhlEntity.zhlPercent+"%").length(), mPercentRect);
        mValuePaint.setTextSize(mValueSize);
        mTitlePaint.setTextSize(mTitleSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        for (int i = 0; i < mZhlEntityList.size(); i++) {
            if (i == 0) {
                mStartLineY = mVerticalSpace + mItemHeight * i;
            } else if (i == mZhlEntityList.size() - 1) {
                mEndLineY = mVerticalSpace + mItemHeight * i;
            }
        }
        canvas.drawLine(mWidth * mWdithRate, mStartLineY, mWidth * mWdithRate, mEndLineY, mLinePaint);

        for (int i = 0; i < mZhlEntityList.size(); i++) {
            ZhlEntity zhlEntity = mZhlEntityList.get(i);
            caculateSize(zhlEntity);
            canvas.drawCircle(mWidth * mWdithRate, mVerticalSpace + mItemHeight * i, mPointRadius, mPointPaint);
            canvas.drawText(zhlEntity.zhlTitle, mWidth * mWdithRate - (mTitleRect.width() + mTitleSpace), mVerticalSpace + mItemHeight * i + mPointRadius, mTitlePaint);
            RectF rectF = new RectF(mWidth * mWdithRate + mRectSpace, mVerticalSpace + mItemHeight * i - mPointRadius * 4 / 3, mWidth * mWdithRate + mTitleSpace + zhlEntity.rectWidth, mVerticalSpace + mItemHeight * i + mPointRadius * 4 / 3);
            canvas.drawRect(rectF, mRectPaint);
            canvas.drawText(zhlEntity.zhlValue+"", rectF.right + mValueSpace, mVerticalSpace + mItemHeight * i + mValueRect.height() / 2, mValuePaint);
            if (i != 0) {
                canvas.drawText(zhlEntity.zhlPercent+"%", mWidth * mWdithRate - (mPercentRect.width() + mTitleSpace), mVerticalSpace + mItemHeight * i - mTitleRect.height()*4/3, mValuePaint);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureWidth(widthMeasureSpec);

        mItemHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mWidth / 15,
                getResources().getDisplayMetrics());
        mHeight = (int) (mItemHeight * (mZhlEntityList.size() - 1)+mVerticalSpace * 2);

        int maxValue = 0;
        for (int i = 0; i < mZhlEntityList.size(); i++) {
            if (maxValue < mZhlEntityList.get(i).zhlValue) {
                maxValue = mZhlEntityList.get(i).zhlValue;
            }
        }
        for (int i = 0; i < mZhlEntityList.size(); i++) {
            ZhlEntity entity = mZhlEntityList.get(i);
            entity.rectWidth = mWidth*mWdithRate*2*entity.zhlValue/maxValue;
        }

        setMeasuredDimension(mWidth, mHeight);
    }

    private int measureWidth(int widthMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = mWidth;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        this.mWidth = result;
        return result;
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = mHeight;
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        this.mHeight = result;
        return result;
    }

    public class ZhlEntity{
        public String zhlTitle;
        public int zhlValue;
        public float zhlPercent;
        public float rectWidth;
    }
}
